package com.tiocloud.chat.feature.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.tiocloud.chat.R;
import com.tiocloud.chat.constant.TioConfig;
import com.tiocloud.chat.constant.TioExtras;
import com.tiocloud.chat.databinding.TioMainActivityBinding;
import com.tiocloud.chat.feature.main.adapter.MainTabPagerAdapter;
import com.tiocloud.chat.feature.main.model.MainTab;
import com.tiocloud.chat.feature.main.mvp.MainContract;
import com.tiocloud.chat.feature.main.mvp.MainPresenter;
import com.tiocloud.jpush.PushLauncher;
import com.watayouxiang.androidutils.page.TioActivity;
import com.watayouxiang.wallet.TioWallet;

/**
 * author : TaoWang
 * date : 2019-12-31
 * desc :
 */
public class MainActivity extends TioActivity implements MainContract.View {

    public MainPresenter presenter;
    private TioMainActivityBinding binding;

    public static void start(Context context) {
        MainActivity.start(context, null);
    }

    /**
     * @param tabIndex 再次打开，希望所处的页签索引
     */
    public static void start(Context context, int tabIndex) {
        Intent intent = new Intent();
        intent.putExtra(TioExtras.EXTRA_TAB_INDEX, tabIndex);
        MainActivity.start(context, intent);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.tio_main_activity);
        presenter = new MainPresenter(this);
        presenter.init();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 再次打开，希望所处的页签索引
        int tabIndex = intent.getIntExtra(TioExtras.EXTRA_TAB_INDEX, -1);
        if (tabIndex != -1) {
            binding.viewPager.setCurrentItem(tabIndex);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 检测更新
        presenter.checkAppUpdate();
        // 清除所有通知
        presenter.clearAllNotifications();
        TioWallet.KEY = TioConfig.HAND_SHAKE_KEY_ONLINE;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
        binding.unbind();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 不关闭应用，返回主界面
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public MainActivity getMainActivity() {
        return this;
    }

    @Override
    public void initViews() {
        // 初始化 pager
        MainTabPagerAdapter adapter = new MainTabPagerAdapter(getSupportFragmentManager(), binding.viewPager, this);
        binding.viewPager.setCurrentItem(MainTab.CHAT.tabIndex, false);
        // 初始化 tab
        binding.tabStrip.setViewPager(binding.viewPager);
        binding.tabStrip.setOnTabClickListener(adapter);
        binding.tabStrip.setOnTabDoubleTapListener(adapter);
    }

    @Override
    public void updateRedDot(int pageIndex, int count) {
        if (binding == null) return;
        binding.tabStrip.updateTab(pageIndex, count);

        // 设置角标数字
        if (pageIndex == MainTab.CHAT.tabIndex) {
            PushLauncher.getInstance().setBadgeNumber(count);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2){
            if (binding == null) return;
            binding.tabStrip.setCurrentPosition(0);
        }
    }
}
